package com.jt.www.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jt.www.common.service.MessageService;
import com.jt.www.dao.mapper.messageinfo.MessageInfoEntityMapper;
import com.jt.www.model.entity.MessageInfoEntity;
import com.jt.www.model.vo.MessageNumVo;
import com.jt.www.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.HashMap;
import java.util.List;

/**
 * @author admin_ltf
 * @create 2019-04-14 13:54
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageInfoEntityMapper messageInfoEntityMapper;

    @Override
    public Integer addMessageInfo(MessageInfoEntity messageInfoEntity) {
        int num = messageInfoEntityMapper.insert(messageInfoEntity);
        return num;
    }

    @Override
    public HashMap<String, Object> searchMessageByTargetUser(String targetUser, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        Weekend<MessageInfoEntity> weekend = Weekend.of(MessageInfoEntity.class);
        WeekendCriteria<MessageInfoEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(MessageInfoEntity::getTargetUser, targetUser);
        criteria.andEqualTo(MessageInfoEntity::getIsDeleted, false);//未删除
        weekend.setOrderByClause("notice_state ASC,create_time DESC");
        List<MessageInfoEntity> messageInfoEntityList = messageInfoEntityMapper.selectByExample(weekend);
//        MessageInfoEntity t = new MessageInfoEntity();
//        t.setTargetUser(targetUser);
//        t.setIsDeleted(false);
//        List<MessageInfoEntity> messageInfoEntityList = messageInfoEntityMapper.select(t);
        messageInfoEntityList.forEach(e -> {
            e.setCreateTime(e.getCreateTime().substring(0, 10));
        });
        PageInfo pageinfo = new PageInfo(messageInfoEntityList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageCount", pageinfo.getTotal());
        map.put("result", messageInfoEntityList);
        return map;
    }

    @Override
    public HashMap<String, Object> searchMessageByTargetOrganization(String targetOrganization, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        Weekend<MessageInfoEntity> weekend = Weekend.of(MessageInfoEntity.class);
        WeekendCriteria<MessageInfoEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(MessageInfoEntity::getTargetOrganization, targetOrganization);
        criteria.andEqualTo(MessageInfoEntity::getIsDeleted, false);//未删除
        weekend.setOrderByClause("notice_state ASC,create_time DESC");
        List<MessageInfoEntity> messageInfoEntityList = messageInfoEntityMapper.selectByExample(weekend);
//        MessageInfoEntity t = new MessageInfoEntity();
//        t.setTargetOrganization(targetOrganization);
//        t.setIsDeleted(false);
//        List<MessageInfoEntity> messageInfoEntityList = messageInfoEntityMapper.select(t);
        PageInfo pageinfo = new PageInfo(messageInfoEntityList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageCount", pageinfo.getTotal());
        map.put("result", messageInfoEntityList);
        return map;
    }

    @Override
    public HashMap<String, Object> searchMessageByTargetArea(String targetArea, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        Weekend<MessageInfoEntity> weekend = Weekend.of(MessageInfoEntity.class);
        WeekendCriteria<MessageInfoEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(MessageInfoEntity::getTargetArea, targetArea);
        criteria.andEqualTo(MessageInfoEntity::getIsDeleted, false);//未删除

        weekend.setOrderByClause("notice_state ASC,create_time DESC");
        List<MessageInfoEntity> messageInfoEntityList = messageInfoEntityMapper.selectByExample(weekend);
//        MessageInfoEntity t = new MessageInfoEntity();
//        t.setTargetArea(targetArea);
//        t.setIsDeleted(false);
//        List<MessageInfoEntity> messageInfoEntityList = messageInfoEntityMapper.select(t);
        PageInfo pageinfo = new PageInfo(messageInfoEntityList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageCount", pageinfo.getTotal());
        map.put("result", messageInfoEntityList);
        return map;
    }

//    @Override
////    public List<MessageInfoEntity> searchAllMessage() {
////        return null;
////    }

    @Override
    public MessageInfoEntity searchMessageById(Integer id) {
        MessageInfoEntity t = new MessageInfoEntity();
        t.setId(id.longValue());
        MessageInfoEntity messageInfoEntity = messageInfoEntityMapper.selectByPrimaryKey(t);
        return messageInfoEntity;
    }

    @Override
    public MessageInfoEntity searchLatestUnreadMessage(String targetUser) {
        // 手写sql 实现
//        MessageInfoEntity messageInfoEntity = messageInfoEntityMapper.searchLatestUnreadMessageByUser(targetUser);

        // 使用框架方法 实现 ：  查询目标用户最新的一条未读消息  排序和分页
        Weekend<MessageInfoEntity> weekend = Weekend.of(MessageInfoEntity.class);
        WeekendCriteria<MessageInfoEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(MessageInfoEntity::getTargetUser, targetUser);
        criteria.andEqualTo(MessageInfoEntity::getIsDeleted, false);//未删除
        criteria.andEqualTo(MessageInfoEntity::getNoticeState, false);//未读
        weekend.setOrderByClause("create_time DESC");
        List<MessageInfoEntity> messageInfoEntities = messageInfoEntityMapper.selectByExample(weekend);
        MessageInfoEntity messageInfoEntity = null;
        if (messageInfoEntities != null && messageInfoEntities.size() > 0) {
            messageInfoEntity = messageInfoEntities.get(0);
        }

        return messageInfoEntity;
    }

//    @Override
//    public HashMap<String, Object> searchUnreadMessageByUser(String targetUser,Integer page, Integer rows) {
//        PageHelper.startPage(page, rows);
//        Weekend<MessageInfoEntity> weekend = Weekend.of(MessageInfoEntity.class);
//        WeekendCriteria<MessageInfoEntity, Object> criteria = weekend.weekendCriteria();
//        criteria.andEqualTo(MessageInfoEntity::getTargetUser,targetUser);
//        criteria.andEqualTo(MessageInfoEntity::getIsDeleted,false);//未删除
//        criteria.andEqualTo(MessageInfoEntity::getNoticeState,false);//未通知
//
//        weekend.setOrderByClause("notice_state ASC,create_time DESC");
//        List<MessageInfoEntity> messageInfoEntityList = messageInfoEntityMapper.selectByExample(weekend);
//
//        PageInfo pageinfo = new PageInfo(messageInfoEntityList);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("pageCount", pageinfo.getTotal());
//        map.put("result", messageInfoEntityList);
//        return map;
//    }

    @Override
    public int updateNoticeStatetById(Integer id) {
        MessageInfoEntity messageInfoEntity = new MessageInfoEntity();
        messageInfoEntity.setId(Long.valueOf(id));
        messageInfoEntity.setNoticeState(true);
        messageInfoEntity.setUpdateTime(DateUtils.getCurrentTime());
        int num = messageInfoEntityMapper.updateByPrimaryKeySelective(messageInfoEntity);
        return num;
    }

    @Override
    public int deleteMessageInfoById(Integer id) {
        MessageInfoEntity messageInfoEntity = new MessageInfoEntity();
        messageInfoEntity.setId(Long.valueOf(id));
        messageInfoEntity.setIsDeleted(true);
        messageInfoEntity.setUpdateTime(DateUtils.getCurrentTime());

        int num = messageInfoEntityMapper.updateByPrimaryKeySelective(messageInfoEntity);

        return num;
    }

    @Override
    public List<MessageNumVo> searchToDoItemByTargetUser(String targetUser) {

        List<MessageNumVo> messageNumVoList = messageInfoEntityMapper.queryToDoItemNum(targetUser);
        return messageNumVoList;
    }

    @Override
    public Integer searchUnreadMessagetotalNumByUser(String targetUser) {

        MessageInfoEntity t = new MessageInfoEntity();
        t.setTargetUser(targetUser);
        t.setIsDeleted(false);
        t.setNoticeState(false);// 通知状态:0-未通知；1-已通知
        int totalNum = messageInfoEntityMapper.selectCount(t);
        return totalNum;
    }
}
